package com.linshengjian.snowball.core;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

@Accessors(chain = true)
@Setter
@Getter
@MappedSuperclass
public abstract class Entity<ID extends Serializable> extends AbstractPersistable<ID> {
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date createTime;

    @Column()
    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updateTime;

    @Column(columnDefinition = "boolean default true")
    private Boolean valid = true;

    @Override
    public void setId(ID id) {
        super.setId(id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getId());
    }
}